package com.example.administrator.szb.activity.myyewu;

import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.android.volley.Request;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.example.administrator.szb.R;
import com.example.administrator.szb.activity.base.BasePresenter;
import com.example.administrator.szb.activity.base.MVPBaseActivity;
import com.example.administrator.szb.bean.ResultBean;
import com.example.administrator.szb.bean.YWGLBean;
import com.example.administrator.szb.tinkerutil.SampleApplicationLike;
import com.example.administrator.szb.http.HttpUtil;
import com.example.administrator.szb.http.HttpUtils;
import com.example.administrator.szb.http.OnResultListener;
import com.example.administrator.szb.http.URLAddress;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

public class YWNOActivity2 extends MVPBaseActivity {

    private LinearLayout ywgl_no_text_show_ll;
    private TextView ywgl_no_text_show;
    private ListView listView;
    private Button btn;
    private TextView bar_title;
    private List<YWGLBean> list = new ArrayList<>();
    private YwAdapter1 adapter;

    private ArrayList<Integer> integerList1_1 = new ArrayList<>();//一级选中标签(有二级标签如股票质押)
    private ArrayList<Integer> integerList1_2 = new ArrayList<>();//一级选中标签(无二级标签如市值管理)
    private ArrayList<Integer> integerList2 = new ArrayList<>();//二级选中标签
    private ArrayList<Integer> integerList3 = new ArrayList<>();//三级选中标签
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_ywno2);
    }

    @Override
    protected BasePresenter createPresenter() {
        return null;
    }


    @Override
    protected void initView() {
        integerList1_1 = getIntent().getIntegerArrayListExtra("list1_1");
        integerList1_2 = getIntent().getIntegerArrayListExtra("list1_2");
        integerList2 = getIntent().getIntegerArrayListExtra("list2");
        integerList3 = getIntent().getIntegerArrayListExtra("list3");
        bar_title = findViewById(R.id.bar_title);
        bar_title.setText("业务管理");
        ywgl_no_text_show_ll = (LinearLayout) findViewById(R.id.ywgl_no_text_show_ll);
        ywgl_no_text_show = (TextView) findViewById(R.id.ywgl_no_text_show);
        listView = (ListView) findViewById(R.id.listView);
        btn = (Button) findViewById(R.id.btn);
    }

    @Override
    protected void initEvent() {
        adapter = new YwAdapter1(this,list);
        adapter.setIntegerList(integerList1_2);
        adapter.setIntegerList2(integerList1_1);
        adapter.setOnItemClickListener(new YwAdapter1.OnItemClickListener() {
            @Override
            public void onItemClick(int pos) {
                ArrayList<Integer> ll = new ArrayList<Integer>();

                for (int j=0;j<list.get(pos).getNext().size();j++){
                    if (integerList2.contains(list.get(pos).getNext().get(j).getId())){
                        ll.add(list.get(pos).getNext().get(j).getId());
                    }
                }
                if (list.get(pos).getLevel()==1){

                    Intent intent = new Intent(activity,YWXQActivity_bgzr_save2.class);
                    intent.putExtra("data",list.get(pos));
                    intent.putExtra("list2",ll);
                    startActivity(intent);
                }else {

                    Intent intent = new Intent(activity,YWXQActivity_save2.class);
                    intent.putExtra("data",list.get(pos));
                    intent.putExtra("list2",ll);
                    intent.putExtra("list3",integerList3);
                    startActivity(intent);
                }
            }
        });
        listView.setAdapter(adapter);
        btn.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                List<Integer> integerList = adapter.getIntegerList();
                String str  = "";
                for (int i=0;i<integerList.size();i++){
                    str+=integerList.get(i)+",";
                }
                if (TextUtils.isEmpty(str)){
                    finish();
                }else {
                    doSubmit(str);
                }

            }
        });
    }

    @Override
    protected void initData() {
        requestData();
    }

    private void doSubmit(String id_info){
        HashMap<String, String> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("users_id", SampleApplicationLike.getUserloginInstance().getId() + "");
        map.put("business_id", id_info);
        map.put("describe", "");


        HttpUtil.RequestGsonPost(SampleApplicationLike.getQueueInstance(), Request.Method.POST, ResultBean.class, URLAddress.URLBASE + URLAddress.URL_YWBQSC, map, new Response.Listener() {
            @Override
            public void onResponse(Object response) {
                ResultBean bean = (ResultBean) response;
                if (bean.getResult() == 1) {
                    if (!MyYeWuActivity.isLoad){
                        startActivity(new Intent(activity, MyYeWuActivity.class));
                    }

                    finish();
                }
            }
        }, new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {

            }
        });
    }

    private void requestData() {
        HashMap<String, Object> map = new HashMap<>();
        map.put("code", HttpUtil.getParam());
        map.put("business_id", "" + 0);

        String urls = URLAddress.URLBASE + URLAddress.URL_BUSINESS2;
        HttpUtils.post(activity,urls, map, 0, new OnResultListener() {
            @Override
            public void onSuccess(int requestCode, String str) {
                str = str.replace("false","0");
                List listtmp = JSON.parseArray(str,YWGLBean.class);
                if (listtmp==null){
                    listtmp = new ArrayList();
                }
                list.clear();
                list.addAll(listtmp);
                adapter.notifyDataSetChanged();
            }

            @Override
            public void onFailure(int requestCode, String str) {

            }
        });
    }
    public static boolean canFinish = false;
    @Override
    protected void onResume() {
        super.onResume();
        if (canFinish){
            if (!MyYeWuActivity.isLoad){
                startActivity(new Intent(activity, MyYeWuActivity.class));
            }
            finish();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        canFinish = false;
    }
}
